package com.wwwarehouse.usercenter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileCheckBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileListBean;
import com.wwwarehouse.usercenter.bean.business_relationship.GetSalesmansAndTagsByBuIdBean;
import com.wwwarehouse.usercenter.fragment.business_relationship.DeskDrawerMultipleTrebleFragment;

import java.util.List;

/**
 * 多选筛选器的Adapter
 * Created by ShineF on 2017/7/11 0011.
 */

public class FilterMultipleTrebleAdapter extends BaseAdapter {
    private Context mContext;
    private List<BRCustomerFileCheckBean.ScreenBean> mList;
    private List<GetSalesmansAndTagsByBuIdBean.ScreeningTagsBean> mList2;
    private List<GetSalesmansAndTagsByBuIdBean.ScreeningSalesMansBean> mList3;
    private ViewHolder mHolder;
    private DeskDrawerMultipleTrebleFragment.OnItemListener onItemListener;

    public FilterMultipleTrebleAdapter(Context context, List<BRCustomerFileCheckBean.ScreenBean> mList, List<GetSalesmansAndTagsByBuIdBean.ScreeningTagsBean> mList2, List<GetSalesmansAndTagsByBuIdBean.ScreeningSalesMansBean> mList3) {
        this.mContext = context;
        this.mList = mList;
        this.mList2 = mList2;
        this.mList3 = mList3;
    }

    @Override
    public int getCount() {
        if (mList != null)
            return mList.size();
        else if (mList2 != null)
            return mList2.size();
        else if (mList3 != null)
            return mList3.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null)
            return mList.get(position);
        else if (mList2 != null)
            return mList2.get(position);
        else if (mList3 != null)
            return mList3.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, com.wwwarehouse.common.R.layout.item_drawer_filter_multiple, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        if (mList != null){
            final BRCustomerFileCheckBean.ScreenBean screeningTagsBean = mList.get(position);
            if (screeningTagsBean.isSelected()) {
                mHolder.mBtn.setSelected(true);
            } else {
                mHolder.mBtn.setSelected(false);
            }

            mHolder.mBtn.setText(screeningTagsBean.getBuName());
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!screeningTagsBean.isSelected()) {
                        for (BRCustomerFileCheckBean.ScreenBean screenBean : mList) {
                            screenBean.setSelected(false);
                        }
                        screeningTagsBean.setSelected(true);
                        if (onItemListener != null)
                            onItemListener.onItemListener(screeningTagsBean.getBuId());
                    }else{
                        screeningTagsBean.setSelected(false);
                        if (onItemListener != null)
                            onItemListener.onItemListener(null);
                    }
                    notifyDataSetChanged();
                }
            });
        }else if (mList2 != null){
            final GetSalesmansAndTagsByBuIdBean.ScreeningTagsBean screeningTagsBean = mList2.get(position);
            if (screeningTagsBean.isSelected()) {
                mHolder.mBtn.setSelected(true);
            } else {
                mHolder.mBtn.setSelected(false);
            }

            mHolder.mBtn.setText(screeningTagsBean.getTagName());
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    screeningTagsBean.setSelected(!screeningTagsBean.isSelected());

                    notifyDataSetChanged();
                }
            });

        }else if (mList3 != null){
            final GetSalesmansAndTagsByBuIdBean.ScreeningSalesMansBean screeningTagsBean = mList3.get(position);
            if (screeningTagsBean.isSelected()) {
                mHolder.mBtn.setSelected(true);
            } else {
                mHolder.mBtn.setSelected(false);
            }

            mHolder.mBtn.setText(screeningTagsBean.getSalesManName());
            mHolder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    screeningTagsBean.setSelected(!screeningTagsBean.isSelected());

                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        Button mBtn;

        public ViewHolder(View view) {
            mBtn = (Button) view.findViewById(com.wwwarehouse.common.R.id.btn_item);
        }
    }

    public DeskDrawerMultipleTrebleFragment.OnItemListener getOnItemListener() {
        return onItemListener;
    }

    public void setOnItemListener(DeskDrawerMultipleTrebleFragment.OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }
}
